//从我的抽奖进入详情
function myLuckToInfo(id) {
  to('../my-lotto/info/info?id=' + id, 0);
}
//进入领奖成功
function toLuckSuccess() {
  to('success', 0);
}
//进入我的抽奖
function toMyLuck(level = 1) {
  to('my-luck/my-luck', level);
}
//进入幸运抽奖详情
function toLuckInfo(id, level = 0) {
  to('info/info?id=' + id, level);
}

//进入规则说明
function toAboutUsRule(level = 1) {
  to('rule/rule', level);
}
//进入我的订单
function toMyOrder(level = 1) {
  to('order/order', level);
}
//进入互助记录
function toHelpRecord(level = 1) {
  to('help/record', level);
}
//进入赞助
function toSponsor(userInfo, type = 0, level = 1) {
  to('sponsor/sponsor?userInfo=' + userInfo + "&type=" + type, level);
}

//进入凑步数海报
function toPoster(level = 1) {
  to('poster/poster', level);
}
//进入商品详情
function toCommodityInfo(id, goods_type, exchange, level = 1) {
  to('commodity/info?id=' + id + "&goods_type=" + goods_type + "&exchange=" + exchange, level);
}

//进入排行榜
function toRank(level = 1) {
  to('rank/rank', level);
}

function toRankRule(rule, level = 0) {
  to('rule/rule?rule=' + rule, level);
}

//进入签到
function toSign(level = 1) {
  to('sign/sign', level);
}
//进入签到规则
function toSignRule(level = 0) {
  to('rule/rule', level);
}
//进入我的卡牌
function toMyCard(level = 0) {
  to('card/card', level);
}
//进入兑换卡牌
function toExchangeCard(level = 0) {
  to('exchange/exchange', level);
}
//进入赠送卡牌验证手机号
function toGive(level = 0) {
  to('give/give', level);
}

function toGiveExecute(userInfo, level = 1) {
  to('give/execute?userInfo=' + JSON.stringify(userInfo), level);
}

//进入h5
function toWeb(title, url, level = 1) {
  to('web/web?title=' + title + "&url=" + url, level);
}
//跳转小程序
function toMiniProgram(appId, path) {
  wx.navigateToMiniProgram({
    appId: appId,
    path: path,
  })
}

function to(name, level = 1) {
  var path = "";
  for (var i = 0; i < level; i++) {
    path += "../";
  }
  path += name;
  wx.navigateTo({
    url: path,
  })
}

//返回首页
function backHome() {
  var pagelist = getCurrentPages();
  var len = pagelist.length;
  var init = 0;
  var index = 0;
  for (var i = 0; i < len; i++) {
    if (pagelist[i].route.indexOf("index/index") >= 0) { //看路由里面是否有首页
      init = 1;
      index = i;
    }
  }
  if (init == 1) {
    wx.navigateBack({
      delta: len - i - 1
    });
  } else {
    wx.reLaunch({
      url: "/pages/index/index" //这个是默认的单页
    });
  }
}



//获取赞助参数
function sponsorPath(openid) {
  return shareParam(1, JSON.stringify({
    openid: openid
  }));
}

function shareParam(type, shareInfo) {
  return 'type=' + type + "&shareInfo=" + shareInfo;
}

module.exports = {
  to: to,
  toSign: toSign,
  toSignRule: toSignRule,
  toMyCard: toMyCard,
  toGive: toGive,
  toGiveExecute: toGiveExecute,
  toMiniProgram: toMiniProgram,
  toWeb: toWeb,
  toRank: toRank,
  toRankRule: toRankRule,
  toExchangeCard: toExchangeCard,
  toCommodityInfo: toCommodityInfo,
  toPoster: toPoster,
  sponsorPath: sponsorPath,
  toSponsor: toSponsor,
  toHelpRecord: toHelpRecord,
  toMyOrder: toMyOrder,
  toAboutUsRule: toAboutUsRule,
  toLuckInfo: toLuckInfo,
  toMyLuck: toMyLuck,
  myLuckToInfo: myLuckToInfo,
  toLuckSuccess: toLuckSuccess,
  backHome:backHome,
}